package com.kensure.shike.order.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;
import co.kensure.mem.NumberUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.baobei.model.SKBaobei;
import com.kensure.shike.baobei.service.SKBaobeiService;
import com.kensure.shike.changshang.model.ChangShang;
import com.kensure.shike.changshang.service.ChangShangService;
import com.kensure.shike.order.dao.SKCarDao;
import com.kensure.shike.order.model.SKCar;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;

/**
 * 购物车 服务实现类
 * 
 * @author fankd created on 2019-7-6
 * @since
 */
@Service
public class SKCarService extends JSBaseService {

	@Resource
	private SKCarDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKUserService sKUserService;
	@Resource
	private SKBaobeiService sKBaobeiService;
	@Resource
	private SKOrderService sKOrderService;
	@Resource
	private ChangShangService changShangService;

	public SKCar selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKCar> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKCar> selectAll() {
		return dao.selectAll();
	}

	public List<SKCar> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCount() {
		return dao.selectCount();
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKCar obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_car"));
		return dao.insert(obj);
	}

	public boolean update(SKCar obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	/**
	 * 购物车新增记录
	 * 
	 * @param car
	 * @return
	 */
	public SKCar add(SKCar car) {
		car.setUserid(sKUserService.getUser().getId());
		insert(car);
		return car;
	}

	/**
	 * 前端，根据用户获取购物车细腻些
	 */
	public List<SKCar> qdlist() {
		SKUser user = sKUserService.getUser();
		Map<String, Object> parameters = MapUtils.genMap("userid", user.getId());
		List<SKCar> list = selectByWhere(parameters);
		if (CollectionUtils.isNotEmpty(list)) {
			for (SKCar order : list) {
				SKBaobei bb = sKBaobeiService.selectOne(order.getBbid());
//				Double salePrice = sKOrderService.getPrice(order);
				order.setBaobei(bb);
//				order.setSalePrice(salePrice);
			}
		}
		return list;
	}
	
	/**
	 * 前端，根据用户获取购物车信息
	 */
	public Collection<ChangShang> qdlist1() {
		SKUser user = sKUserService.getUser();
		Map<String, Object> parameters = MapUtils.genMap("userid", user.getId());
		List<SKCar> list = selectByWhere(parameters);
		Map<Long,ChangShang> csMap = new HashMap<>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (SKCar order : list) {
				SKBaobei bb = sKBaobeiService.selectOne(order.getBbid());
//				Double salePrice = sKOrderService.getPrice(bb, user, null);
				order.setBaobei(bb);
//				order.setSalePrice(salePrice);
				ChangShang changshang = csMap.get(bb.getSjid());
				if(changshang == null){
					changshang = changShangService.selectOne(bb.getSjid());
					List<SKCar> templist = new ArrayList<>();
					changshang.setCarlist(templist);
					csMap.put(bb.getSjid(), changshang);
				}
				changshang.getCarlist().add(order);	
			}
		}
		return csMap.values();
	}

	/**
	 * 前端，删除购物车信息
	 */
	public void del(String ids) {
		String[] idarr = ids.split(",");
		for (String idstr : idarr) {
			long id = NumberUtils.parseLong(idstr, null);
			delete(id);
		}
	}

}
